﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

namespace HRProject.Code.DTO
{
    public class BLWorktimeTable
    {
        private Dictionary<Guid, WorktimeRow> rows;
        private int year;
        private int month;

        public int Month
        {
            get { return month; }
            set { month = value; }
        }

        public int Year
        {
            get { return year; }
            set { year = value; }
        }


        public Dictionary<Guid, WorktimeRow> Rows
        {
            get { return rows; }
        }

        public BLWorktimeTable(int year, int month)
        {
            this.year = year;
            this.month = month;
            rows = new Dictionary<Guid, WorktimeRow>();
        }

        public WorktimeRow AddRow(Guid agentId, string fullName)
        {
            WorktimeRow row = new WorktimeRow(agentId, fullName, year, month);
            rows.Add(agentId, row);
            return row;
        }

        public void SetCell(Guid agentId, string fullName, int day, List<BLWorktime> records, BLWorktimeProperty property)
        {
            WorktimeRow row = null;
            try
            {
                row = rows[agentId];
            }
            catch (KeyNotFoundException knfExp)
            {
                row = AddRow(agentId, fullName);
                Console.Out.WriteLine(knfExp.Message);
            }
            if ((day > 0) && (day <= row.Cells.Count))
            {
                foreach (BLWorktime record in records)
                {
                    row.Cells[day-1].DailyActivities.Add(record);
                }
                row.Cells[day-1].Property = property;
            }
        }
    }

    public class WorktimeRow
    {
        private Guid agentId;
        private string fullName;

        public string FullName
        {
            get { return fullName; }
        }

        public Guid AgentId
        {
            get { return agentId; }
        }
        private List<BLWorktimeRecord> cells;

        public List<BLWorktimeRecord> Cells
        {
            get { return cells; }
        }

        public WorktimeRow(Guid agentId,string fullName, int year, int month)
        {
            this.agentId = agentId;
            this.fullName = fullName;
            cells = new List<BLWorktimeRecord>();
            int daysInMonth = DateTime.DaysInMonth(year, month);
            for (int i = 0; i < daysInMonth; i++)
            {
                cells.Add(new BLWorktimeRecord(agentId, new DateTime(year, month, i + 1)));
            }
        }
    }
}
